Date.prototype.getDayOfYear = function () {
    var start = new Date(this.getFullYear(), 0, 1);
    if (this.getHours() == 0 && this.getMinutes() == 0 && this.getSeconds() == 0) {
        return (this - start) / (24 * 60 * 60 * 1000) + 1;
    }
    return Math.ceil((this - start) / (24 * 60 * 60 * 1000));
}

Date.prototype.getWeekOfYear = function () {
    var originYear = this.getFullYear();
    var start = new Date(this.getFullYear(), 0, 1);
    var startWeekDay = start.getDay();
    var thisWeekDay = this.getDay();
    var yearDay = this.getDayOfYear();
    if (startWeekDay != 0) {
        yearDay += startWeekDay;
    }
    this.setDate(this.getDate() - (thisWeekDay - 6));
    if (this.getFullYear() > originYear) {
        return 1;
    }
    return Math.ceil(yearDay / 7);
}

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

$.fn.datebox.defaults.formatter = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    if (m < 10) {
        m = '0' + m;
    }
    if (d < 10) {
        d = '0' + d;
    }
    return y + "-" + m + "-" + d;
};

function addPrefix(value) {
    if (value < 10) {
        return "0" + value;
    }
    return "" + value;
}

function reduceDays(date, days) {
    date.setDate(date.getDate() - days);
}

function reduceMonth(date, months) {
    date.setMonth(date.getMonth() - months);
}

function getIntDate(value) {
    return parseInt(value.replace(/[^0-9]/ig, ""));
}

function getInitStartWeekValue() {
    var date = new Date();
    var displayWeekNum = 30;
    date.setDate(date.getDate() - displayWeekNum * 7);
    return date.getFullYear() + "-" + addPrefix(date.getWeekOfYear());
}

function getInitEndWeekValue() {
    var date = new Date();
    var displayWeekNum = 1;
    date.setDate(date.getDate() - displayWeekNum * 7);
    return date.getFullYear() + "-" + addPrefix(date.getWeekOfYear());
}

function getInitStartMonthValue() {
    var date = new Date();
    var displayMonthNum = 30;
    date.setMonth(date.getMonth() - displayMonthNum);
    return date.getFullYear() + "-" + addPrefix(date.getMonth() + 1);
}

function getInitEndMonthValue() {
    var date = new Date();
    var displayMonthNum = 1;
    date.setMonth(date.getMonth() - displayMonthNum);
    return date.getFullYear() + "-" + addPrefix(date.getMonth() + 1);
}

function getInitStartQuarterValue() {
    var date = new Date();
    var displayQuarterNum = 30;
    date.setMonth(date.getMonth() - displayQuarterNum * 3);
    return date.getFullYear() + "-" + addPrefix(parseInt((date.getMonth() + 1) / 3));
}

function getInitEndQuarterValue() {
    var date = new Date();
    var month = parseInt(date.getMonth()) + 1;
    if (month <= 3) {
        return (date.getFullYear() - 1) + "-04";
    }
    return date.getFullYear() + "-" + addPrefix(parseInt((month - 1) / 3));
}

function preDay(date) {
    var ds = date.replace(/-/ig, "/");
    var d = new Date(ds);
    reduceDays(d, 1);
    return d.format("yyyy-MM-dd");
}

function nextDay(value) {
    var ds = value.replace(/-/ig, "/");
    var d = new Date(ds);
    reduceDays(d, -1);
    return d.format("yyyy-MM-dd");
}

function nextWeek(value) {
    var date = (value + "").replace(/-/ig, "");
    var year = date.substring(0, 4);
    var week = date.substring(4, 6);
    if (parseInt(week) > 51) {
        var yearLastDate = new Date(year + "/12/31");
        var weekDay = yearLastDate.getDay();
        if (weekDay != 6) {
            reduceDays(yearLastDate, parseInt(weekDay) + 1);
        }
        var weekNum = yearLastDate.getWeekOfYear();
        if (week == weekNum) {
            return (parseInt(year) + 1) + "-01";
        }
    }
    return year + "-" + addPrefix((parseInt(week) + 1));
}

function nextMonth(value) {
    var date = (value + "").replace(/-/ig, "");
    var year = date.substring(0, 4);
    var month = date.substring(4, 6);
    if (month == 12) {
        return (parseInt(year) + 1) + "-01";
    }
    return year + "-" + addPrefix((parseInt(month) + 1));
}

function nextQuarter(value) {
    var date = (value + "").replace(/-/ig, "");
    var year = date.substring(0, 4);
    var quarter = date.substring(4, 6);
    if (quarter == 4) {
        return (parseInt(year) + 1) + "-01";
    }
    return year + "-0" + (parseInt(quarter) + 1);
}

/*获取日期的前n个季度值，value格式"YYYY-MM-DD"*/
function getQuarterNum(value, n) {
    var date = (value + "").replace(/-/ig, "/");
    var d = new Date(date);
    var reduceMonths = n * 3;
    reduceMonth(d, reduceMonths);
    var quarter = parseInt((d.getMonth() - 1) / 3) + 1;
    return quarter;
}

function getQuarters() {
    var date = new Date();
    reduceMonth(date, 2);
    var year = date.getFullYear();
    var month = date.getMonth();
    var result=[];
    for (var i = parseInt((month - 1)/ 3); i > 0; i--) {
        result.push(year + "-0" + i);
    }
    for (var y = year - 1; y >= 2013; y--) {
        for (var i = 4; i > 0; i--) {
            result.push(y + "-0" + i);
        }
    }
    return result;
}


//日期转换格式转换----函数
function formatDate(date,format) {
	var o = {
	    "M+" : date.getMonth()+1, //month
	    "d+" : date.getDate(),    //day
	    "h+" : date.getHours(),   //hour
	    "m+" : date.getMinutes(), //minute
	    "s+" : date.getSeconds(), //second
	    "q+" : Math.floor((date.getMonth()+3)/3),  //quarter
	    "S" : date.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(date.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o) if(new RegExp("("+ k +")").test(format))
		format = format.replace(RegExp.$1,
				RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));
	return format;
	
}


function formatterDate(date) {
	  var seperator1 = "-";
	  var month = date.getMonth() + 1;
	  var strDate = date.getDate();
	  if (month >= 1 && month <= 9) {
	      month = "0" + month;
	  }
	  if (strDate >= 0 && strDate <= 9) {
	      strDate = "0" + strDate;
	  }
	  var currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
	  return currentDate;
}
